package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.mapper.DictDetailMapper;
import com.crm.context.BaseContext;
import com.crm.dto.DictDetailDto;
import com.crm.entity.Dict;
import com.crm.entity.DictDetail;
import com.crm.result.PageResult;
import com.crm.service.DictDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDetail> implements DictDetailService {
    @Autowired
    private DictDetailMapper dictDetailMapper;

    @Override
    public PageResult pageDict(DictDetailDto dictDetailDto) {
        Page<DictDetail> page = Page.of(dictDetailDto.getPageNum(), dictDetailDto.getPageSize());

        //分页条件
        LambdaQueryWrapper<DictDetail> wrapper = new LambdaQueryWrapper<DictDetail>();
        wrapper.like(dictDetailDto.getDictType()!=null,DictDetail::getDictType,dictDetailDto.getDictType());
        wrapper.like(dictDetailDto.getDictLabel()!=null,DictDetail::getDictLabel,dictDetailDto.getDictLabel());
        wrapper.eq(dictDetailDto.getStatus()!=null,DictDetail::getStatus,dictDetailDto.getStatus());



        super.page(page,wrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void add(DictDetailDto dictDetailDto) {
        DictDetail dictDetail = new DictDetail();
        //设置状态（默认为0）
        dictDetail.setStatus(Dict.DICT_TYPE_STATUS_OPEN);

        BeanUtils.copyProperties(dictDetailDto,dictDetail);
        //设置创建时间
        dictDetail.setCreateTime(LocalDateTime.now().toString());

        //设置创建人
        Long currentId = BaseContext.getCurrentId();
        dictDetail.setCreateBy(currentId != null ? currentId.toString() : null);

        dictDetailMapper.insert(dictDetail);

    }

//

    @Override
    public DictDetail selectDictById(Long dictCode) {
        LambdaQueryWrapper<DictDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDetail::getDictCode,dictCode);
        DictDetail dictDetail = dictDetailMapper.selectOne(wrapper);
        return dictDetail;
    }

    @Override
    public void updateDict(DictDetail dictDetail) {
        LambdaQueryWrapper<DictDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDetail::getDictCode,dictDetail.getDictCode());
        dictDetailMapper.update(dictDetail,wrapper);
    }



    @Override
    public void deleteBatchByIds(List<Long> ids) {
        if(!CollectionUtils.isEmpty(ids)){
            for (Long id : ids) {
                dictDetailMapper.delete(Wrappers.<DictDetail>lambdaQuery().eq(DictDetail::getDictCode, id));
            }
        }

    }

    @Override
    public List<DictDetail> selectDictByType(String dictType) {
        LambdaQueryWrapper<DictDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(dictType),DictDetail::getDictType,dictType);
        List<DictDetail> dictDetails = dictDetailMapper.selectList(wrapper);
        return dictDetails;
    }
}
